package com.woniuxy.certified.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.certified.dao.CertifiedAccount;
import com.woniuxy.certified.model.dto.CertifiedAccountDto;
import com.woniuxy.certified.model.dto.CertifiedAccountInfoDto;
import com.woniuxy.certified.model.form.LoadAllAccountForm;
import com.woniuxy.certified.model.form.LoginForm;
import com.woniuxy.certified.model.form.RegisterForm;
import com.woniuxy.certified.model.form.UpdatePasswordForm;
import com.woniuxy.certified.model.param.*;
import com.woniuxy.certified.service.CertifiedAccountService;
import com.woniuxy.core.util.PageInfo;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.util.JwtUtil;
import com.woniuxy.core.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 凌玉春
 * @since 2024年01月15日
 */
@RestController
@RequestMapping("/certifiedAccount")
@Api(tags = "物业账号接口")
public class CertifiedAccountController {


    @Resource
    private CertifiedAccountService certifiedAccountService;

    @Resource
    private RedisUtil redisUtil;


    @ApiOperation("物业管理员登录")
    @PostMapping("/login")
    private Result<CertifiedAccountDto> login(@RequestBody LoginForm lf, HttpServletResponse response) throws Exception {
        LoginParam param = BeanUtil.toBean(lf, LoginParam.class);
        CertifiedAccountDto dto = certifiedAccountService.login(param,response);
       return Result.getResult(CertifiedAccountDto.class).setMessage("登录成功").setData(dto);
    }

    @ApiOperation("物业管理员新增")
    @PostMapping("/RegisterAccount")
    private Result<CertifiedAccountDto> RegisterAccount(@RequestBody RegisterForm rf, @RequestHeader String token){
        String accountId = JwtUtil.parseAdminToken(token).get("id").toString();
        String certifiedInfoId = JwtUtil.parseAdminToken(token).get("certifiedInfoId").toString();
        RegisterParam param = BeanUtil.toBean(rf, RegisterParam.class);
        param.setAccountId(accountId);
        param.setCertifiedInfoId(certifiedInfoId);
        CertifiedAccountDto dto = certifiedAccountService.RegisterAccount(param);
        return Result.getResult(CertifiedAccountDto.class).setMessage("新增物业管理员成功").setData(dto);
    }


    @ApiOperation("超级管理员删除普通管理员")
    @GetMapping("/deleteAccount")
    @ApiImplicitParam(name = "certifiedAccountId",value = "普通管理员id")
    public Result deleteAccount(Integer certifiedAccountId,@RequestHeader String token){
        String accountRole = JwtUtil.parseAdminToken(token).get("accountRole").toString();
        String certifiedInfoId = JwtUtil.parseAdminToken(token).get("certifiedInfoId").toString();
        DeleteAccountParam param = new DeleteAccountParam();
        param.setCertifiedAccountId(certifiedAccountId);
        param.setAccountRole(Integer.valueOf(accountRole));
        param.setCertifiedInfoId(Integer.valueOf(certifiedInfoId));
        certifiedAccountService.certifiedAccountService(param);
        return Result.getResult().setMessage("删除账号成功");
    }


    @ApiOperation("超级管理员禁用普通管理员")
    @GetMapping("/bannedAccount")
    public Result bannedAccount(Integer certifiedAccountId,@RequestHeader String token){
        String accountRole = JwtUtil.parseAdminToken(token).get("accountRole").toString();
        String certifiedInfoId = JwtUtil.parseAdminToken(token).get("certifiedInfoId").toString();
        BannedAccountParam  param = new BannedAccountParam();
        param.setCertifiedAccountId(certifiedAccountId);
        param.setAccountRole(Integer.valueOf(accountRole));
        param.setCertifiedInfoId(Integer.valueOf(certifiedInfoId));
        certifiedAccountService.bannedAccount(param);
        return Result.getResult().setMessage("禁用管理员成功");
    }

    @ApiOperation("超级管理员启用普通管理员")
    @GetMapping("/enableAccount")
    public Result enableAccount(Integer certifiedAccountId,@RequestHeader String token){
        String accountRole = JwtUtil.parseAdminToken(token).get("accountRole").toString();
        String certifiedInfoId = JwtUtil.parseAdminToken(token).get("certifiedInfoId").toString();
        BannedAccountParam  param = new BannedAccountParam();
        param.setCertifiedAccountId(certifiedAccountId);
        param.setAccountRole(Integer.valueOf(accountRole));
        param.setCertifiedInfoId(Integer.valueOf(certifiedInfoId));
        certifiedAccountService.enableAccount(param);
        return Result.getResult().setMessage("启用管理员成功");
    }


    @ApiOperation("修改密码")
    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody UpdatePasswordForm upf, @RequestHeader String token){
        String id = JwtUtil.parseAdminToken(token).get("id").toString();
        UpdatePasswordParam param = BeanUtil.toBean(upf, UpdatePasswordParam.class);
        param.setCertifiedAccountId(Integer.valueOf(id));
        certifiedAccountService.updatePassword(param);
        return Result.getResult().setMessage("修改密码成功");
    }

    @ApiOperation("超级管理员设置管理员为超级管理员")
    @GetMapping("/setSuperAdmin")
    public Result setSuperAdmin(Integer accountId,@RequestHeader String token){
        String id = JwtUtil.parseAdminToken(token).get("id").toString();
        certifiedAccountService.setSuperAdmin(id,accountId);
        return Result.getResult().setMessage("设置超级管理员成功");
    }

    @ApiOperation("超级管理员设置管理员为普通管理员")
    @GetMapping("/setNormalAdmin")
    public Result setNormalAdmin(Integer accountId,@RequestHeader String token){
        String id = JwtUtil.parseAdminToken(token).get("id").toString();
        certifiedAccountService.setNormalAdmin(id,accountId);
        return Result.getResult().setMessage("设置普通管理员成功");
    }

    @ApiOperation("获取当前账号信息")
    @GetMapping("/getAccountInformation")
    public Result<CertifiedAccountInfoDto> getAccountInformation(@RequestHeader String token){
        String id = JwtUtil.parseAdminToken(token).get("id").toString();
        CertifiedAccountInfoDto dto = certifiedAccountService.getAccountInformation(id);
        return Result.getResult(CertifiedAccountInfoDto.class).setMessage("获取当前账号信息成功").setData(dto);
    }

    @ApiOperation("获取账号信息")
    @GetMapping("/getAccountInfo")
    public Result<CertifiedAccountInfoDto> getAccountInfo(String id){
        CertifiedAccountInfoDto dto = certifiedAccountService.getAccountInformation(id);
        return Result.getResult(CertifiedAccountInfoDto.class).setMessage("获取当前账号信息成功").setData(dto);
    }


    @ApiOperation("获取某个物业公司所有账号")
    @PostMapping("/loadAllAccount")
    public Result<PageInfo> loadAllAccount(@RequestBody LoadAllAccountForm laaf, @RequestHeader String token){
        String id = JwtUtil.parseAdminToken(token).get("id").toString();
        LoadAllAccountParam param = BeanUtil.toBean(laaf, LoadAllAccountParam.class);
        PageInfo<CertifiedAccountInfoDto> pageInfo = certifiedAccountService.loadAllAccount(param,id);
        return Result.getResult(PageInfo.class).setMessage("加载所有账号成功").setData(pageInfo);
    }

    @ApiOperation("平台通过id修改物业账号状态")
    @GetMapping("/updatecertifiedstate")
    public Result updatecertifiedstate(String id){
        CertifiedAccount certifiedAccount= certifiedAccountService.updatecertifiedstate(id);
        return Result.getResult(CertifiedAccount.class).setMessage("平台修改物业账号状态成功").setData(certifiedAccount);
    }


}

